<?php
App::uses('AppController', 'Controller');
/**
 * Evaluations Controller
 *
 * @property Evaluation $Evaluation
 */
class EvaluationsController extends AppController {
public $uses = array( 'Evaluation', 'Plantilla','CriteriosEvaluation','GroupUser','Group','Upload','Preview', 'EvaluationsUser' );
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$uid = $this->Session->read('Auth.User.id');
		$role = $this->Session->read('Auth.User.role_id');	
		if($role == '6'){
			$this->Evaluation->recursive = 0;				
			$this->set('evaluations', $this->paginate(array("or" => array('Evaluation.user_id LIKE' => $uid))));
		}
		elseif($role == '1') {
			$this->Evaluation->recursive = 0;
		$this->set('evaluations', $this->paginate());
		}
	}

/**
 * index method
 *
 * @return void
 */
	public function index2() {
		$this->Evaluation->Group->recursive = 0;
		$this->set('groupsUsers',$this->GroupUser->find('all'));
		$this->set('groups', $this->paginate('Group'));
	}	



/**
 * index method
 *
 * @return void
 */
	public function indexNotas() {
		$this->Evaluation->Group->recursive = 0;
		$this->set('groupsUsers',$this->GroupUser->find('all'));
		$this->set('groups', $this->paginate('Group'));
		$this->set('evaluationsUsers', $this->paginate('EvaluationsUser'));
	}	


/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Evaluation->exists($id)) {
			throw new NotFoundException(__('Invalid evaluation'));
		}
		$options = array('conditions' => array('Evaluation.' . $this->Evaluation->primaryKey => $id));
		$this->set('evaluation', $this->Evaluation->find('first', $options));
		// paso solamente las evaluaciones de criterios correspondientes al id
		$options = array('conditions' => array('CriteriosEvaluation.evaluation_id'=>$id));
		$this->set('criteriosEvaluations',$this->Evaluation->CriteriosEvaluation->find('all', $options));		
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Evaluation->create();
			if ($this->Evaluation->save($this->request->data)) {
				$this->Session->setFlash(__('la evaluación ha sido guardada'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se ha podido guardar la evaluación, pruebe otra vez'));
			}
		}
		$institutions = $this->Evaluation->Institution->find('list');
		$previews = $this->Evaluation->Preview->find('list');
		$users = $this->Evaluation->User->find('list');
		$plantillas = $this->Evaluation->Plantilla->find('list');
		$criterios = $this->Evaluation->Criterio->find('list');
		$this->set(compact('institutions', 'previews', 'users', 'plantillas', 'criterios'));
	}
	public function crear() {
		$this->request->data['Evaluation']['user_id']=$this->Session->read('Auth.User.id');
		if ($this->request->is('post')) {
			$this->Evaluation->create();
			if ($this->Evaluation->save($this->request->data)) {
				$this->redirect(array('action' => 'evaluar',$this->Evaluation->id, $this->data['Evaluation']['plantilla_id']));
				#$this->redirect('/evaluations/evaluar/'.$this->Evaluation->id);
			} else {
			}
		}
		$institutions = $this->Evaluation->Institution->find('list');
		$previews = $this->Evaluation->Preview->find('list');
		$users = $this->Evaluation->User->find('list');
		$plantillas = $this->Evaluation->Plantilla->find('list');
		$criterios = $this->Evaluation->Criterio->find('list');
		$groups = $this->Evaluation->Group->find('list');
		$this->set(compact('institutions', 'previews', 'users', 'plantillas', 'criterios','groups'));
	}
		public function crear2($grupo,$institucion,$tipo, $grupousuario,$preview) {
		$this->request->data['Evaluation']['group_id'] = $grupo;
		$this->request->data['Evaluation']['institution_id'] = $institucion;
		$this->request->data['Evaluation']['tipo'] = $tipo;
		if(!($preview==0)):
			$this->request->data['Evaluation']['preview_id'] = $preview;
		endif;
		$this->request->data['Evaluation']['user_id']=$this->Session->read('Auth.User.id');
		if ($this->request->is('post')) {
			$this->Evaluation->create();
			if ($this->Evaluation->save($this->request->data)) {
				$this->redirect(array('action' => 'evaluar',$this->Evaluation->id, $this->data['Evaluation']['plantilla_id']));
				#$this->redirect('/evaluations/evaluar/'.$this->Evaluation->id);
			} else {
			}
		}
		$institutions = $this->Evaluation->Institution->find('list');
/*		try {
    		$options = array('conditions' => array('Project.user_group_id',$grupousuario));
			$projects = $this->Evaluation->Project->find('list',$options);    
		} catch(Exception $e) {
    		$this->Session->setFlash(__('Error el grupo no tiene asociado ningun proyecto'));
    		$this->redirect(array('action' => 'index2'));
		}*/

        $previews = $this->Evaluation->Preview->find('list');
		$users = $this->Evaluation->User->find('list');
		$plantillas = $this->Evaluation->Plantilla->find('list');
		$criterios = $this->Evaluation->Criterio->find('list');
		$groups = $this->Evaluation->Group->find('list');
		$this->set(compact('institutions', 'previews', 'users', 'plantillas', 'criterios','groups'));
	}
	public function crear3($usuario) {
		$this->request->data['Evaluation']['user_id']=$this->Session->read('Auth.User.id');
       	$options2 = array('conditions' => array('Preview.user_id='.$usuario));
        $this->Preview->order = 'Preview.id DESC';
        $avance=$this->Preview->find('first',$options2);
        if(!empty($avance['Preview'])):
        	$id2=$avance['Preview']['id'];
            $this->request->data['Evaluation']['preview_id'] = $id2;
    	endif;
		if ($this->request->is('post')) {
			$this->Evaluation->create();
			if ($this->Evaluation->save($this->request->data)) {
				$this->redirect(array('action' => 'evaluar',$this->Evaluation->id, $this->data['Evaluation']['plantilla_id']));
				#$this->redirect('/evaluations/evaluar/'.$this->Evaluation->id);
			} else {
			}
		}
		$institutions = $this->Evaluation->Institution->find('list');
		$previews = $this->Evaluation->Preview->find('list');
		$users = $this->Evaluation->User->find('list');
		$plantillas = $this->Evaluation->Plantilla->find('list');
		$criterios = $this->Evaluation->Criterio->find('list');
		$groups = $this->Evaluation->Group->find('list');
		$this->set(compact('institutions', 'previews', 'users', 'plantillas', 'criterios','groups'));
	}	
	public function evaluar($id = null, $plantilla_id) {

		if (!$this->Evaluation->exists($id)) {
			throw new NotFoundException(__('Evaluación inválida'));
		}
		$options = array('conditions' => array('Evaluation.' . $this->Evaluation->primaryKey => $id));
		$this->set('evaluation', $this->Evaluation->find('first', $options)); 
		$options = array('conditions' => array('Plantilla.' . $this->Plantilla->primaryKey =>$plantilla_id));
		$this->set('plantilla', $this->Evaluation->Plantilla->find('first', $options));
	}


/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Evaluation->exists($id)) {
			throw new NotFoundException(__('Evaluación inválida'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Evaluation->save($this->request->data)) {
				$this->Session->setFlash(__('la evaluación ha sido guardada'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se ha podido guardar la evaluación, pruebe otra vez'));
			}
		} else {
			$options = array('conditions' => array('Evaluation.' . $this->Evaluation->primaryKey => $id));
			$this->request->data = $this->Evaluation->find('first', $options);
		}
		$institutions = $this->Evaluation->Institution->find('list');
		$previews = $this->Evaluation->Preview->find('list');
		$users = $this->Evaluation->User->find('list');
		$plantillas = $this->Evaluation->Plantilla->find('list');
		$criterios = $this->Evaluation->Criterio->find('list');
		$this->set(compact('institutions', 'previews', 'users', 'plantillas', 'criterios'));
	}
	/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function retro($id = null) {
		if (!$this->Evaluation->exists($id)) {
			throw new NotFoundException(__('Evaluación inválida'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Evaluation->save($this->request->data)) {				
				$this->Session->setFlash(__('la evaluación ha sido guardada'));
		$grupo=$this->data['Evaluation']['group_id'];
                $this->redirect(array('action' => 'volver',$this->request->data['Evaluation']['id'], $this->request->data['Evaluation']['group_id']));				
			} else {
				$this->Session->setFlash(__('No se ha podido guardar la evaluación, pruebe otra vez'));
			}
		} else {
			$options = array('conditions' => array('Evaluation.' . $this->Evaluation->primaryKey => $id));
			$this->request->data = $this->Evaluation->find('first', $options);
		}
		$institutions = $this->Evaluation->Institution->find('list');
		$previews = $this->Evaluation->Preview->find('list');
		$users = $this->Evaluation->User->find('list');
		$plantillas = $this->Evaluation->Plantilla->find('list');
		$criterios = $this->Evaluation->Criterio->find('list');
		$options = array('conditions' => array('CriteriosEvaluation.evaluation_id'=>$id));
		$criteriosEvaluations=$this->Evaluation->CriteriosEvaluation->find('all', $options);
		$groups = $this->Evaluation->Group->find('list');
		// filtro a todos los alumnos que pertenecen a un grupo en la lista alumnos para posteriormente ser evaluados
//		$options2 = array('conditions' => array('GroupUser.group_id'=>$grupo));
//		$alumnos=$this->Evaluation->User->find('all', $options2);
		$options = array('conditions' => array('Evaluation.' . $this->Evaluation->primaryKey => $id));
		$evaluation=$this->Evaluation->find('first', $options);
//		print_r($alumnos);
		$this->set(compact('institutions', 'previews', 'users', 'plantillas', 'criterios','criteriosEvaluations','evaluation','groups'));
	}

	public function volver($id = null, $grupo) {

		if (!$this->Evaluation->exists($id)) {
			throw new NotFoundException(__('Evaluación inválida'));
		}
		$options = array('conditions' => array('Evaluation.' . $this->Evaluation->primaryKey => $id));
		$this->set('evaluation', $this->Evaluation->find('first', $options)); 
		$this->set('alumnos',$this->Evaluation->User->query("select * from users u inner join group_users g on g.user_id=u.id where g.group_id=".$grupo));
	}	

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Evaluation->id = $id;
		if (!$this->Evaluation->exists()) {
			throw new NotFoundException(__('Evaluación inválida'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Evaluation->delete()) {
			$this->Session->setFlash(__('Evaluación eliminada'));
			$this->redirect(array('action' => 'index2'));
		}
		$this->Session->setFlash(__('No se ha podido eliminar la evaluación'));
		$this->redirect(array('action' => 'index'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function ver_avance($usuario) {
      
    $options2 = array('conditions' => array('Preview.user_id='.$usuario));
    $this->Preview->order = 'Preview.id DESC';
    $avance=$this->Preview->find('first',$options2);
    if(!empty($avance['Preview'])):
        $id2=$avance['Preview']['id'];
		$this->redirect(array('controller'=>'previews','action' => 'view',$id2));	
	else:
    	$this->Session->setFlash(__('debe seleccionar el avance manualmente'));
    			$this->redirect(array('controller'=>'previews','action' => 'index'));
    endif;

	}
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function boton_evaluar($grupo,$institucion,$tipo, $grupousuario,$usuario) {

		$options = array('conditions' => array('Upload.user_id='.$usuario));
        $this->Upload->order='Upload.id DESC';
		$subida=$this->Upload->find('first', $options);
		if(!empty($subida['Upload'])):
        	$id="'".$subida['Upload']['id']."'";       
        	$options2 = array('conditions' => array('Preview.upload_id='.$id));
        	$this->Preview->order = 'Preview.id DESC';
        	$avance=$this->Preview->find('first',$options2);
        	if(!empty($avance['Preview'])):
        		$id2=$avance['Preview']['id'];
				$this->redirect(array('action' => 'crear2',$grupo,$institucion,$tipo, $grupousuario,$id2));	
			else:
    			$this->Session->setFlash(__('debe seleccionar el avance manualmente'));
    			$this->redirect(array('action' => 'crear2',$grupo,$institucion,$tipo, $grupousuario,0));
    		endif;
    	else:
     		$this->Session->setFlash(__('debe seleccionar el avance manualmente'));
    		$this->redirect(array('action' => 'crear2',$grupo,$institucion,$tipo, $grupousuario,0));   										
	    endif;

	}			
}
